home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5940 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.4 KB

  1. Path: bubba.NMSU.Edu!usenet
  2. From: ghenniga@ampere.NMSU.Edu (Gary Hennigan)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: accessing structures (newbie question)
  5. Date: 21 Feb 1996 09:52:17 -0700
  6. Organization: New Mexico State University - Electromagnetics Group
  7. Sender: ghenniga@ampere.NMSU.Edu
  8. Message-ID: <rhthgwk1v0e.fsf@ampere.NMSU.Edu>
  9. References: <4g8gic$o6u@news1.sunbelt.net>
  10. Reply-To: ghenniga@NMSU.Edu
  11. NNTP-Posting-Host: ampere.nmsu.edu
  12. In-reply-to: dking@SunBelt.Net's message of 19 Feb 1996 00:34:20 GMT
  13. X-Newsreader: Gnus v5.1
  14.  
  15. In an article dking@SunBelt.Net wrote:
  16. >Ok. having trouble accessing structure. here I define a pointer and
  17. >malloc the memory:
  18. >
  19. >struct picture *photos;
  20. >photos = (struct picture *)malloc(1000 * sizeof(picture));
  21. >
  22. >now I try to store data into structures...
  23. >
  24. > for (x=0;x<num_of_files;x++)
  25. >   {
  26. >       strncpy(photos->filename, filelist[x],12);
  27. >       strncpy(photos->diskname, inputdisk,12);
  28. >       strncpy(photos->indexname, inputindex,12);
  29. >   }
  30. >
  31. >now I try to read data...
  32. >
  33. >for (x=0;x<num_of_files;x++)
  34. >{
  35. >
  36. >  printf("%sdisk%sindex%s\n",photos->filename,
  37. >         photos->diskname,photos->indexname);
  38. >}
  39. >
  40. >Obviously I'm pointing to the first of the 1000 structures the entire time. 
  41. >Now for the 60,000 dollar question: HOW do I access the rest of the 
  42. >structures? My tutuorial doesnt have any examples of accessing  MALLOCed 
  43. >structures. The one example on structures defines it as an array (struct 
  44. >picture *photos[x] ) but 1000 elements is too large an array and the
  45. >example 
  46.  
  47. You probably meant:
  48.     struct picture photos[x];
  49.  
  50. Quite different from
  51.     struct picture *photos[x];
  52.  
  53. >with malloc uses floating point data accessed by using (photos[x]) which 
  54. >doesnt work either with the strnccpy or with the -> operator.
  55. >
  56. >I'd Appreciate any assistance you might offer.
  57.  
  58. Easiest way:
  59.  
  60. for(x=0; x < num_of_files; x++)
  61. {
  62.    strncpy((photos+x)->filename, filelist[x], 12);
  63.    strncpy((photos+x)->diskname, inputdisk, 12);
  64.    strncpy((photos+x)->indexname, inputindex, 12);
  65. }
  66.  
  67. similiar for your printf()
  68.  
  69. Also valid:
  70.  
  71. for(x=0; x < num_of_files; x++)
  72. {
  73.    strncpy(photos[x].filename, filelist[x], 12);
  74.    strncpy(photos[x].diskname, inputdisk, 12);
  75.    strncpy(photos[x].indexname, inputindex, 12);
  76. }
  77.  
  78. This assumes you have allocated, or statically declared, all the
  79. character array members within the structure appropriately.
  80.  
  81. You need to brush up on pointers and indirect/direct addressing.
  82.  
  83. Gary
  84. (ghenniga@NMSU.Edu)
  85.